package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.CompanyNews;
import com.base.po.voip.Users;
import com.base.service.CompanyNewsService;
import com.base.service.UsersService;
import com.base.utils.Page;


/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_qiyedongtai {
	@Autowired
	private CompanyNewsService companyNewsService;
	@Autowired
	private UsersService usersService;
	
	
	// 用户首页
	@RequestMapping("/content_qiyedongtai")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		
		List<CompanyNews> companyNews= null;
		switch (adminUsergrade) {
		//  1. 判断是否是admin用户
		case 0:
			page.setCurrentPageNo(1);
			page.setRecordTotal(companyNewsService.countByAll(null, null,null, null, null, null));
			companyNews = companyNewsService.selectByAll(page, null, null, null, null, null, null);
			break;
		//  2. 判断是否是OEM用户
		case 1:
			page.setCurrentPageNo(1);
			page.setRecordTotal(companyNewsService.countByParentid(adminUserId, null, null, null, null, null, null));
			companyNews = companyNewsService.selectByParentid(page, adminUserId, null, null, null, null, null, null);
			break;
		//  2. 判断是否是商家用户
		case 2:
			page.setCurrentPageNo(1);
			page.setRecordTotal(companyNewsService.countByShopidNotAll(adminUserId, null, null, null, null, null, null));
            companyNews = companyNewsService.selectByShopidNotAll(page, adminUserId, null, null, null, null, null, null);
			break;
		}
		/*
		 * 设置分页
		 */
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		
		if (companyNews != null) {
			for(CompanyNews s : companyNews) {
				Users pUsers = usersService.selectByPrimaryKey(s.getParentid());
				if(pUsers != null) {
					s.setParentid(pUsers.getUsername());
				}
				Users rUsers = usersService.selectByPrimaryKey(s.getReceiveid());
				if(rUsers != null) {
					s.setReceiveid(rUsers.getUsername());
				}
			}
			
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_qiyedongtai");
			request.setAttribute("adminUsergrade", adminUsergrade);
			request.setAttribute("list", companyNews);
			return modelAndView;
		}
	
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;

	}
	
	@RequestMapping("/selectCompanyNews")
	public ModelAndView selectCompanyNews(HttpServletRequest request, String id,Integer curPage,
			String username,String parentid,String receiveid,String timeFrom,String timeTo,String title) throws Exception {
	
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<CompanyNews> companyNews= null;
		
		Integer choose = 2;
		if (username != null && !username.equals("")|| username != null && !username.equals("") 
				|| parentid != null && !parentid.equals("") || receiveid != null && !receiveid.equals("")
				|| timeFrom != null && !timeFrom.equals("") && timeTo != null && !timeTo.equals("")
				|| title != null && !title.equals("")) {
			choose = 1;
		}
		String sUsername = null;
		String sParentid = null;
		String sReceiveid = null;
		String sTimeFrom = null;
		String sTimeTo = null;
		String sTitle = null;
		
		if(username != null && !username.equals("")) {
			sUsername = username;
		}
		if(title != null && !title.equals("")) {
			sTitle = title;
		}
		if(parentid != null && !parentid.equals("")) {
			List<Users> pId = usersService.selectByUserName(parentid);
			
			if(pId != null && pId.size()>0) {
				sParentid = pId.get(0).getId();
			}
		}
		if(receiveid != null && !receiveid.equals("")) {
			List<Users> rId = usersService.selectByUserName(receiveid);
			
			if(rId != null && rId.size()>0) {
				sReceiveid = rId.get(0).getId();
			}
		}
		if(timeFrom != null && !timeFrom.equals("") && timeTo != null && !timeTo.equals("")) {
			sTimeFrom = timeFrom + " 00:00:00";
			sTimeTo = timeTo+" 23:59:59";
		}
		
		System.out.println(sTimeFrom);
		System.out.println(sTimeTo);
		page.setCurrentPageNo(curPage);
		if (adminUsergrade == 0) {
			page.setRecordTotal(companyNewsService.countByAll(sParentid, sReceiveid, sTimeFrom, sTimeTo, sUsername, sTitle));
			companyNews = companyNewsService.selectByAll(page, sParentid, sReceiveid, sTimeFrom, sTimeTo, sUsername, sTitle);
		}
		
		if (adminUsergrade == 1) {
			page.setRecordTotal(companyNewsService.countByParentid(adminUserId, sParentid, sReceiveid, sTimeFrom, sTimeTo, sUsername, sTitle));
			companyNews = companyNewsService.selectByParentid(page, adminUserId, sParentid, sReceiveid, sTimeFrom, sTimeTo, sUsername, sTitle);
		}
		
        if (adminUsergrade == 2) {
        	page.setRecordTotal(companyNewsService.countByShopidNotAll(adminUserId, sParentid, sReceiveid, sTimeFrom, sTimeTo, sUsername, sTitle));
			companyNews = companyNewsService.selectByShopidNotAll(page, adminUserId, sParentid, sReceiveid, sTimeFrom, sTimeTo, sUsername, sTitle);
		}
		
        if(choose == 2) {
        	switch (adminUsergrade) {
    		//  1. 判断是否是admin用户
    		case 0:
    			page.setCurrentPageNo(1);
    			page.setRecordTotal(companyNewsService.countByAll(null, null,null, null, null, null));
    			companyNews = companyNewsService.selectByAll(page, null, null, null, null, null, null);
    			break;
    		//  2. 判断是否是OEM用户
    		case 1:
    			page.setCurrentPageNo(1);
    			page.setRecordTotal(companyNewsService.countByParentid(adminUserId, null, null, null, null, null, null));
    			companyNews = companyNewsService.selectByParentid(page, adminUserId, null, null, null, null, null, null);
    			break;
    		//  2. 判断是否是商家用户
    		case 2:
    			page.setCurrentPageNo(1);
    			page.setRecordTotal(companyNewsService.countByShopidNotAll(adminUserId, null, null, null, null, null, null));
                companyNews = companyNewsService.selectByShopidNotAll(page, adminUserId, null, null, null, null, null, null);
    			break;
    		}
        }
        
		/*
		 * 设置分页
		 */
        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

		if (companyNews != null) {
			for(CompanyNews s : companyNews) {
				Users pUsers = usersService.selectByPrimaryKey(s.getParentid());
				if(pUsers != null) {
					s.setParentid(pUsers.getUsername());
				}
				Users rUsers = usersService.selectByPrimaryKey(s.getReceiveid());
				if(rUsers != null) {
					s.setReceiveid(rUsers.getUsername());
				}
			}
			
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_qiyedongtai");
			request.setAttribute("username", username);
			request.setAttribute("parentid", parentid);
			request.setAttribute("receiveid", receiveid);
			request.setAttribute("timeFrom", timeFrom);
			request.setAttribute("timeTo", timeTo);
			request.setAttribute("list", companyNews);
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}
